package com.forclass.zonghe;

import java.lang.reflect.*;
import java.util.Arrays;

public class ReflectionDemo{
    public static void main(String[] args) throws Exception {
        // --------------------- 1. 获取类信息 ---------------------
        System.out.println("*****1. 获取类信息*********");
        Class<?> userClass = Class.forName("com.forclass.zonghe.User");
        System.out.println("类名: " + userClass.getName());
        System.out.println("简单类名: " + userClass.getSimpleName());
        System.out.println("是否为接口: " + userClass.isInterface());

        // --------------------- 2. 操作字段 ---------------------
        // 获取所有字段（包括私有）
        System.out.println("*****2. 操作字段*********");
        Field[] fields = userClass.getDeclaredFields();
        System.out.println("\n字段列表:");
        Arrays.stream(fields).forEach(f -> System.out.println(f.getName() + " : " + f.getType()));

        // 修改私有字段值
        User user = createInstance(userClass);
        Field ageField = userClass.getDeclaredField("age");
        ageField.setAccessible(true); // 突破私有访问限制
        ageField.set(user, 25);
        System.out.println("\n修改后的 age: " + ageField.get(user));

        // --------------------- 3. 调用方法 ---------------------
        // 调用公共方法
        System.out.println("*****3. 调用方法*********");
        Method setNameMethod = userClass.getMethod("setName", String.class);
        setNameMethod.invoke(user, "Alice");

        // 调用私有方法
        Method validateMethod = userClass.getDeclaredMethod("validate");
        validateMethod.setAccessible(true);
        validateMethod.invoke(user);

        // --------------------- 4. 构造对象 ---------------------
        // 调用私有构造方法
        System.out.println("*****4. 构造对象*********");
        Constructor<?> privateConstructor = userClass.getDeclaredConstructor();
        privateConstructor.setAccessible(true);
        User user2 = (User) privateConstructor.newInstance();
        System.out.println("\n通过私有构造方法创建的对象: " + user2);

        // --------------------- 5. 泛型处理 ---------------------
        System.out.println("*****5. 泛型处理*********");
        Method genericMethod = userClass.getMethod("getGenericList");
        Type returnType = genericMethod.getGenericReturnType();
        if (returnType instanceof ParameterizedType) {
            ParameterizedType pType = (ParameterizedType) returnType;
            Type[] actualTypes = pType.getActualTypeArguments();
            System.out.println("\n泛型类型: " + actualTypes[0]);
        }

        // --------------------- 6. 数组操作 ---------------------
        System.out.println("*****6. 数组操作*********");
        Object usersArray = Array.newInstance(userClass, 3);
        Array.set(usersArray, 0, user);
        System.out.println("\n数组第一个元素: " + Array.get(usersArray, 0));
    }
    // 通过公共构造方法创建实例
    private static User createInstance(Class<?> clazz) throws Exception {
        Constructor<?> constructor = clazz.getConstructor(Long.class, String.class);
        return (User) constructor.newInstance(1L, "John");
    }
}
